<?php 
require_once 'pages/TMSBackOfficePage.inc';
require_once 'models/acl.inc';
require_once 'security/captcha.inc';

class TMSAuthPage extends TMSBackOfficePage {
/**
 * @var ICaptchaProvider
 */	
	protected $_ioc_captcha_provider_;
	
	public function __get($nm){
		switch ($nm){
			case 'CaptchaUrl':return $this->Url(null,'Captcha');break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function http_Captcha(){
		$this->CaptchaProvider->SendCaptcha('auth');
	}
	
	public function http_Login($login,$pwd,$captcha){
		if (!$this->CaptchaProvider->CheckCaptcha('auth', $captcha)){
				throw new TExposableException('Invalid captcha!');
		}
		
		if ($this->service->Acl->Login($login,$pwd)){
			$url = $this->service->Url('');
			/*if ($this->request->src){
				$url = $this->request->src;
				$this->SetCookie('src', null);
			}*/
			$this->Application()->Redirect($url);
		} else
			throw new TExposableException('Authentication failed due to invalid login or password!');
	}
	
	public function http_Logout(){
		$this->service->Acl->Logout();
	}
	/*
	public function Handle(THttpRequest $request){
		if (isset($request->src)){
			$this->SetCookie('src', $request->src);
		}
		parent::Handle($request);
	}*/
}
